let logIndex = 0;
let bgIndex = 1;
function nextbg() {
    if(bgIndex >= 41){
        bgIndex = 1;
    }else {
        bgIndex++;
    }
    $("#body").css("background-image","url(/img/imgbg" + bgIndex + ".jpg)");
}
function lastbg() {
    if(bgIndex <= 1){
        bgIndex = 41;
    }else {
        bgIndex--;
    }
    $("#body").css("background-image","url(/img/imgbg" + bgIndex + ".jpg)");
}
function getLog()
{
    //发送一个json请求到后台
    $.ajax({
        type: "POST",
        url: "/getLog",
        contentType:'application/json',
        data: JSON.stringify({
            logIndex
        }),
        success: function (newLog) {
            console.log(newLog);
            logIndex = newLog.index;
            $("#info").append(newLog.newLog);
            if(newLog.needClear){
                //清空当局日志
                $("#info").empty();
            }
        },
        dataType: "json"
    });
}
function showLogin()
{
    let username = document.getElementById("username").value;
    //发送一个json请求到后台
    $.ajax({
        type: "POST",
        url: "/load",
        contentType:'application/json',
        data: JSON.stringify({
            username
        }),
        success: function (roomInfo) {
            $("#player").empty();
            let map = roomInfo.map;//解析房间里的用户信息map
            let demandName = roomInfo.demandName;//当前房间正在等待要牌的用户
            let status = roomInfo.status;//房间状态
            let inGameCount = roomInfo.inGameCount;//在游戏中人数
            if(status != "发牌中") {
                $("#roomStatus").html(status);
            }else{
                $("#roomStatus").html(status  + " <b style='color: #ecd47c'>当前要牌人: " + demandName + "</b>");
                $("#deal").html("发牌给:" + demandName);
            }
            if(status == "等待开牌"){
                //清除发牌阶段产生的操作菜单
                $("#next").remove();
                $("#deal").remove();
            }
            if(status == "开牌中"){
                //清除发牌阶段产生的操作菜单
                //todo  右侧div添加换庄和重开按钮
            }
            $.each(map,function (index,userInfo) {
                console.log(userInfo);
                let lastDom = "#player";//设置结尾的dom元素 以便能在末尾添加
                //卡片为空时，展示空白图案,不为空时根据是否可见进行处理
                let underCard1 = "empty";
                let underCard2 = "empty";
                let frontCard1 = "empty";
                let frontCard2 = "empty";
                let frontCard3 = "empty";
                if(userInfo.underCard1 != null){
                    if(userInfo.underCard1.view){
                        underCard1 = userInfo.underCard1.url;
                    }else{
                        underCard1 = "back";
                    }
                }
                if(userInfo.underCard2 != null){
                    if(userInfo.underCard2.view){
                        underCard2 = userInfo.underCard2.url;
                    }else{
                        underCard2 = "back";
                    }
                }
                if(userInfo.frontCard1 != null){
                    if(userInfo.frontCard1.view){
                        frontCard1 = userInfo.frontCard1.url;
                    }else{
                        frontCard1 = "back";
                    }
                }
                if(userInfo.frontCard2 != null){
                    if(userInfo.frontCard2.view){
                        frontCard2 = userInfo.frontCard2.url;
                    }else{
                        frontCard2 = "back";
                    }
                }
                if(userInfo.frontCard3 != null){
                    if(userInfo.frontCard3.view){
                        frontCard3 = userInfo.frontCard3.url;
                    }else{
                        frontCard3 = "back";
                    }
                }
                let boss = userInfo.boss ? "<b style='color: red'>庄家</b>:" : "<b style='color: lightskyblue'>闲家</b>:";
                let everyUsername = userInfo.username + "";
                if(roomInfo.bossName == username){
                    $("#next").remove();//防止庄家退出切换庄家后错误出现的停止要牌按钮
                    if(status == "准备中" && $("#start").length == 0 && inGameCount > 1){//至少2人才能開始游戲
                        $("#showAllCard").remove();//清除亮牌按钮
                        $("#openCard").remove();
                        $("#menu").append("<a id=\"start\" onclick=\"return start()\">开始游戏&nbsp;&nbsp;&nbsp;</a>");
                    }
                    if(status == "发牌中" && $("#deal").length == 0){
                        $("#start").remove();
                        $("#menu").append("<a id=\"deal\" onclick=\"return deal()\">发牌给:" + demandName + "&nbsp;&nbsp;&nbsp;</a>");
                    }
                    if(status == "等待开牌" && $("#openCard").length == 0){
                        $("#start").remove();
                        $("#menu").append("<a id=\"openCard\" onclick=\"return openCard()\">进入开牌阶段&nbsp;&nbsp;&nbsp;</a>");
                    }
                    if(status == "开牌中" && $("#menu").children().length < inGameCount + 1 && !userInfo.boss){//只生成一轮,且庄家不生成
                        $("#menu").append("<a class='kaipai' onclick=\"return kaipai('" + everyUsername +"')\">开<span style='color: #d419e2'>" + everyUsername + "</span>的牌&nbsp;&nbsp;&nbsp;</a>");
                        $("#changeBoss").append("<br><a onclick=\"return changeBoss('" + everyUsername +"')\">把庄家给<span style='color: #d419e2'>" + everyUsername + "</span>&nbsp;&nbsp;&nbsp;</a>");
                    }
                    if(userInfo.identity == "点数炸了"){
                        //庄家点数炸了

                    }
                }else{
                    $("#start").remove();
                    $("#openCard").remove();
                    //当前要牌人可以选择停止要牌
                    if(username == demandName && status == "发牌中" && $("#next").length == 0 && $("#showAllCard").length == 0 ){
                        $("#menu").append("<a id=\"next\" onclick=\"return next()\">停止要牌&nbsp;&nbsp;&nbsp;</a>" +
                            "<a id=\"showAllCard\" onclick=\"return showAllCard()\">亮牌&nbsp;&nbsp;&nbsp;</a>");
                    }
                    if(status == "准备中"){
                        $("#showAllCard").remove();//清除亮牌按钮
                    }
                }
                if(username == everyUsername && userInfo.underCard1!=null && userInfo.underCard2!=null){
                    underCard1 = userInfo.underCard1.url;
                    underCard2 = userInfo.underCard2.url;
                }
                if(username == roomInfo.clearStop){
                    if($("#next").length != 0){
                        $("#next").remove();
                    }
                }
                // $(lastDom).append("        <div id=\"" + everyUsername + "\" class=\"col-md-6 panel panel-primary\">\n" +
                $(lastDom).append("        <div id=\"" + everyUsername + "\" class=\"col-md-6 userCard\">\n" +
                "            <h4>" + boss + "<span style='color: antiquewhite'>" + everyUsername + "</span>" + "</h4>\n" +
                "            <span class=\"underCard1\"><img src=\"/img/" + underCard1 + ".jpg\"></span>\n" +
                "            <span class=\"underCard2\"><img src=\"/img/" + underCard2 + ".jpg\"></span>\n" +
                "            <span class=\"frontCard1\"><img src=\"/img/" + frontCard1 + ".jpg\"></span>\n" +
                "            <span class=\"frontCard2\"><img src=\"/img/" + frontCard2 + ".jpg\"></span>\n" +
                "            <span class=\"frontCard3\"><img src=\"/img/" + frontCard3 + ".jpg\"></span>\n" +
                "            <span class=\"identity\"><b>" + userInfo.identity + "</b></span>\n" +
                "        </div>");
                lastDom = "#"+everyUsername;//变更结尾的dom元素 以便能在末尾添加
            })
        },
        dataType: "json"
    });
}
let interval = setInterval("showLogin()","200");
let getLogInterval = setInterval("getLog()","100");

function showAllCard() {
    let username = document.getElementById("username").value;
    $.ajax({
        type: "POST",
        url: "/showAllCard",
        contentType:'application/json',
        data: JSON.stringify({
            username
        }),
        success: function (roomInfo) {
            //亮牌后去掉停止亮牌的按钮
            $("#next").remove();
            $("#showAllCard").remove();
        },
        dataType: "json"
    });
}
function changeBoss(username) {
    $.ajax({
        type: "POST",
        url: "/changeBoss",
        contentType:'application/json',
        data: JSON.stringify({
            username
        }),
        success: function (roomInfo) {
            //清空更换庄家区
            $("#changeBoss").empty();
            //移除菜单区的按钮
            $(".kaipai").remove();
            $("#dealMe").remove();
        },
        dataType: "json"
    });
}

function restart() {
    $.ajax({
        type: "POST",
        url: "/restart",
        contentType:'application/json',
        success: function (roomInfo) {
            //清空更换庄家区
            $("#changeBoss").empty();
            //移除菜单区的按钮
            $(".kaipai").remove();
            $("#dealMe").remove();
        },
        dataType: "json"
    });
}

function kaipai(username) {
    $.ajax({
        type: "POST",
        url: "/kaipai",
        contentType:'application/json',
        data: JSON.stringify({
            username
        }),
        success: function (result) {
            //todo 分数比较
            let status = result.status;
            let winnerName = result.winnerName;
            if(winnerName != username){
                //庄家赢
            }else{
                if(status == "庄家爆"){

                }
                if(status == "给庄家干了！"){

                }
            }
        },
        dataType: "json"
    });
}

function deal() {
    $.ajax({
        type: "POST",
        url: "/deal",
        contentType:'application/json',
        success: function (roomInfo) {

        },
        dataType: "json"
    });
}

function next() {
    $("#next").remove();
    let username = document.getElementById("username").value;
    $.ajax({
        type: "POST",
        url: "/next",
        contentType:'application/json',
        data: JSON.stringify({
            username
        }),
        success: function (roomInfo) {

        },
        dataType: "json"
    });
}

function start() {
    let username = document.getElementById("username").value;
    $.ajax({
        type: "POST",
        url: "/start",
        contentType:'application/json',
        data: JSON.stringify({
            username
        }),
        success: function (roomInfo) {

        },
        dataType: "json"
    });
    return false;
}

function openCard() {
    //进入开牌阶段后 取消发牌和进入开牌阶段按钮
    $("#deal").remove();
    $("#openCard").remove();
    if($("#dealMe").length == 0) {
        $("#menu").append("<a id=\"dealMe\" onclick=\"return deal()\">发牌给自己</a>&nbsp;&nbsp;&nbsp;");
    }
    if($("#restart").length == 0) {
        $("#changeBoss").append("<a id='restart' onclick=\"return restart()\">开始下一局</a>&nbsp;&nbsp;&nbsp;");
    }
    let username = document.getElementById("username").value;
    $.ajax({
        type: "POST",
        url: "/openCard",
        contentType:'application/json',
        data: JSON.stringify({
            username
        }),
        success: function (roomInfo) {
        },
        dataType: "json"
    });
    return false;
}
